
package protocol.transactions;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.Subject;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;

public class GetSubjectDetailTransaction extends XoneBaseTransaction {

    private int mSubjectId;
    private String mSubjectName;

    public GetSubjectDetailTransaction(int subjectId, String subjectName) {
        super(TRANSACTION_TYPE_GET_SUBJECT_DETAIL);
        mSubjectId = subjectId;
        mSubjectName = subjectName;
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetSubjectDetailRequest(mSubjectId,
                mSubjectName);
        sendRequest(request);
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj != null && obj instanceof JsonElement) {
            JsonElement jsonSubject = ((JsonElement)obj).getAsJsonObject().get("subject");
            Gson gson = new Gson();
            Subject subject = gson.fromJson(jsonSubject, Subject.class);
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, subject);
        } else {
            // TODO
            // notifyError(errCode, arg3)
        }
    }

}
